package org.example.controller;

import org.example.common.BaseResponse;
import org.example.common.ResultUtils;
import org.example.domain.User;
import org.example.exception.ErrorCode;
import org.example.exception.ThrowUtils;
import org.example.manager.ModelManager;
import org.example.service.RecommendService;
import org.example.vo.RecommendVO;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

import static org.example.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/recommend")
public class RecommendController {
    @Resource
    private RecommendService recommendService;

    @GetMapping("/get")
    public BaseResponse<List<RecommendVO>> getRecommend(@RequestParam String message, HttpServletRequest request){
        // 获取用户信息
        User user = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_LOGIN_ERROR);
        List<RecommendVO> byMessage = recommendService.getByMessage(message);
        System.out.println("byMessage = " + byMessage);
        if(byMessage != null)
            return ResultUtils.success(byMessage);
        // 创建一个临时的推荐列表（如果您的数据库中确实没有数据）
        List<RecommendVO> dummyRecommends = new ArrayList<>();

        RecommendVO rec1 = new RecommendVO();
        rec1.setId(1);
        rec1.setTravelName("西湖风景区");
        rec1.setTravelMessage("西湖，位于浙江省杭州市，是中国著名的风景名胜区，也是中国首批国家重点风景名胜区之一。");
        rec1.setTravelPhoto("/assets/xihupic.jpg");
        dummyRecommends.add(rec1);

        RecommendVO rec2 = new RecommendVO();
        rec2.setId(2);
        rec2.setTravelName("故宫博物院");
        rec2.setTravelMessage("故宫博物院，位于北京中轴线上，是中国明清两代的皇家宫殿，无与伦比的古代建筑杰作。");
        rec2.setTravelPhoto("/assets/gugongpic.jpg");
        dummyRecommends.add(rec2);

        RecommendVO rec3 = new RecommendVO();
        rec3.setId(3);
        rec3.setTravelName("长城");
        rec3.setTravelMessage("长城，中国古代的伟大防御工程，是中华民族的象征之一，也是世界上最伟大的建筑之一。");
        rec3.setTravelPhoto("/assets/changchengpic.jpg");
        dummyRecommends.add(rec3);

        // 返回硬编码的推荐列表
        return ResultUtils.success(dummyRecommends);
    }
}
